package com.newangels.behaviour;

import jade.core.Agent;
import jade.core.behaviours.CyclicBehaviour;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;

import static com.newangels.util.BaseUtil.killContainerOf;

/**
 * 关闭Agent的行为
 *
 * @author: TangLiang
 * @date: 2020/5/15 14:08
 * @since: 1.0
 */
public class AgentManageBehaviour extends CyclicBehaviour {
    private Agent agent;

    public AgentManageBehaviour(Agent a) {
        this.agent = a;
    }

    public static AgentManageBehaviour getAgentManageBehaviour(Agent a) {
        return new AgentManageBehaviour(a);
    }

    @Override
    public void action() {
        MessageTemplate mt = MessageTemplate.and(
                MessageTemplate.MatchPerformative(ACLMessage.REQUEST),
                MessageTemplate.MatchConversationId("AgentManageFromMyGatewayAgent"));
        ACLMessage msg = agent.receive(mt);
        if (msg != null) {
            String message = msg.getContent();
            switch (message) {
                case "killAgent":
                    killContainerOf(agent);
                    break;
                case "suspendAgent":
                    agent.doSuspend();
                    break;
            }
        } else {
            block();
        }
    }
}
